package team.bluepen.order.data.database.hive;

import space.lingu.light.*;
import team.bluepen.order.data.entity.Order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author RollW
 */
@Dao
@SuppressWarnings("unused")
public abstract class OrderDao implements DaoConnectionGetter {
    @Query("SELECT * FROM order_table")
    public abstract List<Order> get();

    @Query("SELECT * FROM order_table WHERE order_valid = {valid}")
    public abstract List<Order> get(boolean valid);

    @Query("SELECT * FROM order_table WHERE order_name = {name}")
    public abstract List<Order> get(String name);

    @Query("SELECT * FROM order_table WHERE order_date = {date}")
    public abstract List<Order> getByDate(String date);

    @Query("SELECT * FROM order_table WHERE order_id = {id}")
    public abstract List<Order> get(long id);

    // 加载数据使用LOAD DATA
    public void loadData(String path, boolean isLocal) {
        SharedConnection sharedConnection = getConnection();
        Connection connection = getConnection().acquire();
        StringBuilder sqlBuilder = new StringBuilder("LOAD DATA ");
        if (isLocal) {
            sqlBuilder.append("LOCAL ");
        }
        sqlBuilder.append("INPATH '")
                .append(path)
                .append("' OVERWRITE INTO TABLE order_table");
        try {
            PreparedStatement stmt = connection.prepareStatement(sqlBuilder.toString());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new LightRuntimeException(e);
        }

        sharedConnection.release(connection);
    }
}
